package Controllers;


import Bean.BeanOrderDetail;

import Impl.OrderDetailServiceImpl;

import com.alibaba.fastjson.JSONArray;
import exception.BooksNotFoundException;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.util.Collection;

@Controller
public class ShowAllStudentsController extends HttpServlet {

    private static final long serialVersionUID = 1L;
    OrderDetailServiceImpl orderDetailService;
    @RequestMapping("/ShowStudents")
    protected void handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
        // TODO Auto-generated method stub
        response.setContentType("text/html;charset=UTF-8");
        JSONArray arr = new JSONArray();
        ApplicationContext applicationContext=(ApplicationContext)request.getSession().getServletContext().getAttribute("ctx");
        orderDetailService=(OrderDetailServiceImpl) applicationContext.getBean("OrderDetailServiceImpl");
        Collection<BeanOrderDetail> orderDetails;
        int id=Integer.parseInt(request.getParameter("id").replaceAll("'", ""));
        try {
            orderDetails = orderDetailService.getStudentsByClass(id);
            for(BeanOrderDetail orderDetail:orderDetails) {
                arr.add(orderDetail.toJson());
            }
        } catch (BooksNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        response.getWriter().append(arr.toJSONString());
    }
}
